package cn.meibang.meibanguserandclient.provider.dao;

import cn.meibang.meibanguserandclient.provider.base.BaseMeibangUserAndClientSqlProvider;
import cn.meibang.meibanguserandclient.provider.request.ListMeibangUserAndClientRequest;
import cn.meibang.meibanguserandclient.provider.request.MeibangUserAndClientRequest;
import org.apache.ibatis.jdbc.SqlBuilder;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import static org.apache.ibatis.jdbc.SqlBuilder.*;

/**
 * 业务管理客 dao sql 层
 *
 * @author yudw
 * @version 1.0
 */

public class MeibangUserAndClientSqlProvider extends BaseMeibangUserAndClientSqlProvider {

    public String lisForPage(ListMeibangUserAndClientRequest request) {
        BEGIN();
        getSelected();
        FROM("meibang_user_and_client a ");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());

        if (request.getPage() != null && request.getSize() != null) {
            sql.append(" limit #{index,jdbcType=INTEGER} ,  #{size,jdbcType=INTEGER} ");
        }

        return sql.toString();
    }

    public String inserAll(Map<String, List<MeibangUserAndClientRequest>> map) {
        List<MeibangUserAndClientRequest> list = map.get("list");
        StringBuilder stringBuilder = new StringBuilder(256);
        stringBuilder.append(
            "insert into meibang_user_and_client (master_id,user_id,reason,remark,create_date,update_date,create_by,update_by,del_flag) values ");
        MessageFormat messageFormat = new MessageFormat(
            "(#'{'list[{0}].masterId,jdbcType=BIGINT},#'{'list[{0}].userId,jdbcType=BIGINT},#'{'list[{0}].reason,jdbcType=VARCHAR},#'{'list[{0}].remark,jdbcType=VARCHAR},#'{'list[{0}].createDate,jdbcType=DATE},#'{'list[{0}].updateDate,jdbcType=DATE},#'{'list[{0}].createBy,jdbcType=BIGINT},#'{'list[{0}].updateBy,jdbcType=BIGINT},#'{'list[{0}].delFlag,jdbcType=VARCHAR})");
        for (int i = 0; i < list.size(); i++) {
            stringBuilder.append(messageFormat.format(new Integer[] {i}));
            stringBuilder.append(",");
        }
        stringBuilder.setLength(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

    public String list(MeibangUserAndClientRequest request) {
        BEGIN();
        getSelected();
        FROM("meibang_user_and_client a");
        getComonJoin(request);
        SqlBuilder.WHERE(" a.del_flag =  #{delFlag,jdbcType=CHAR}");
        StringBuilder sql = new StringBuilder(SqlBuilder.SQL());
        return sql.toString();
    }

    public void getSelected() {
        SELECT(
            "id as id,master_id as masterId,user_id as userId,reason as reason,remark as remark,create_date as createDate,update_date as updateDate,create_by as createBy,update_by as updateBy,del_flag as delFlag");
    }

    public void getComonJoin(MeibangUserAndClientRequest request) {

        if (request.getId() != null) {
            SqlBuilder.WHERE(" a.id =#{id}");
        }

        if (request.getIds() != null) {
            SqlBuilder.WHERE(" a.id in (" + request.getIds() + ")");
        }
    }
}